package com.ymatou.iapi.userregister.testcase;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.userregister.parameter.ReguserEmailActivateBean;
import com.ymatou.iapi.userregister.parameter.SendToEmailBean;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.ReguserEmailActivateCall;
import com.ymatou.iapi.userregister.service.SendToEmailCall;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.validatecode.api.parameter.GetValidateCodeBean;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.ValidateCodeService;
import com.ymttest.database.model.YmtValidationCode;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.YmtValidationCodeWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 邮件激活 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ReguserEmailActivate {
	private static ReguserEmailActivateBean reguseremailactivateBean;
	private static ReguserEmailActivateCall reguseremailactivateCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("邮件激活");
	}

	@Before
	public void caseUp() {
		reguseremailactivateBean = new ReguserEmailActivateBean();
		reguseremailactivateCall = new ReguserEmailActivateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(reguseremailactivateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReguserEmailActivate_001() {
		Logger.start(true, "邮件激活成功(不输入type)");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();

			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(true, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_ReguserEmailActivate_002() {
		Logger.start(true, "不输入id,失败");
		try {
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("false", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("400", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_ReguserEmailActivate_003() {
		Logger.start(true, "输入错误格式id,失败");
		try {
			reguseremailactivateBean.setId("1");
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("false", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("400", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_ReguserEmailActivate_004() {
		Logger.start(true, "输入type不正确,进入默认处理");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();

			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateBean.setType("abc");
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(true, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_ReguserEmailActivate_005() {
		Logger.start(true, "输入type为onlyverify,验证激活码");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();

			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateBean.setType("onlyverify");
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(false, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");

			reguseremailactivateCall.callService();
			Logger.verifyEquals("false", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("400", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("激活码已失效", reguseremailactivateCall.getString("Result"), "验证Result");
			
			validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(500, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_ReguserEmailActivate_006() {
		Logger.start(true, "输入type为decode,返回激活码信息");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();

			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateBean.setType("decode");
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(0, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(0, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(false, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");

			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals(regbean.getLoginMail() + "|" + userId, reguseremailactivateCall.getString("Result"), "验证Result");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_ReguserEmailActivate_007() {
		Logger.start(true, "邮件已激活再次激活失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();

			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(true, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");

			reguseremailactivateCall.callService();
			Logger.verifyEquals("false", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("400", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("激活码已失效", reguseremailactivateCall.getString("Result"), "验证Result");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_ReguserEmailActivate_008() {
		Logger.start(true, "未查询到用户,错误");
		try {
			// UserRegisterInfoRequestDtoBean regbean =
			// JUserCallService.initUserRegisterInfoRequestDtoBean();
			// UserRegisterInfoRequestDtoCall urrc =
			// JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			//
			// JsonObject registerResult =
			// urrc.getJsonObject().get("Result").getAsJsonObject();
			// String token=registerResult.get("Token").getAsString();
			// Integer userId = urrc.getUserid();
			String mail = TestDataManager.getEmail();
			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(mail);
			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();

			String id = getEncodeId(activeCode, mail, -1);
			reguseremailactivateBean.setId(id);
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("false", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("400", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(mail, getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_ReguserEmailActivate_009() {
		Logger.start(true, "onlyverify后再次激活,成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();

			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateBean.setType("onlyverify");
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(false, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");

			getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
			activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();
			reguseremailactivateCall=new ReguserEmailActivateCall();
			id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateBean.setType(null);
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(true, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_ReguserEmailActivate_010() {
		Logger.start(true, "decode后再次激活,成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();

			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateBean.setType("decode");
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(0, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(0, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(false, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");
			
			Logger.comment("再次激活");
			reguseremailactivateBean.setType(null);
			reguseremailactivateCall=new ReguserEmailActivateCall();
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), getValidateCodeBean.getValidateType()).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(true, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReguserEmailActivate_011() {
		Logger.start(true, "商家邮件激活成功(走总线)");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegisterType(1);
			regbean.setEmailRegister(true);
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
//			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
//			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();
			Thread.sleep(3000);
			YmtValidationCode validationCode=new YmtValidationCode();
			List<YmtValidationCode> validationCodelist=new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), null);
			if(validationCodelist.size()>0){
				validationCode=validationCodelist.get(0);
			}else{
				Thread.sleep(3000);
				validationCode=new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), null).get(0);
			}
			String activeCode=validationCode.getsValidateCode();
			
			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			
			validationCode=new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), null).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(true, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReguserEmailActivate_012() {
		Logger.start(true, "商家邮件激活成功(调用sendtomail)");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
//			regbean.setRegisterType(1);
//			regbean.setEmailRegister(true);
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			
			this.sendMail(userId, regbean.getLoginMail(), regbean.getLoginId());
//			GetValidateCodeBean getValidateCodeBean = ValidateCodeService.setGetValidateCodeBean(regbean.getLoginMail());
//			String activeCode = ValidateCodeService.GetValidateCode(getValidateCodeBean).getValidateCode();
			Thread.sleep(3000);
			YmtValidationCode validationCode=new YmtValidationCode();
			List<YmtValidationCode> validationCodelist=new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), null);
			if(validationCodelist.size()>0){
				validationCode=validationCodelist.get(0);
			}else{
				Thread.sleep(3000);
				validationCode=new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), null).get(0);
			}
			String activeCode=validationCode.getsValidateCode();
			
			String id = getEncodeId(activeCode, regbean.getLoginMail(), userId);
			reguseremailactivateBean.setId(id);
			reguseremailactivateCall.setData(reguseremailactivateBean);
			reguseremailactivateCall.callService();
			Logger.verifyEquals("true", reguseremailactivateCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", reguseremailactivateCall.getString("ResponseCode"), "验证ResponseCode");
			
			validationCode=new YmtValidationCodeWapper().getByActionUserAndValidateType(regbean.getLoginMail(), null).get(0);
			Logger.verifyEquals(activeCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(1, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(200, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");

			Logger.verifyEquals(true, new JUserWapper().selectUserinfoByUid(userId).get(0).getIsActivateEmail(), "验证邮件激活成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * 获取加密id
	 * @param activeCode
	 * @param email
	 * @param userId
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 * @throws UnsupportedEncodingException
	 * @throws GeneralSecurityException
	 */
	private String getEncodeId(String activeCode, String email, Integer userId)
			throws NoSuchAlgorithmException, InvalidKeySpecException, UnsupportedEncodingException, GeneralSecurityException {
		String codeid = activeCode + "|" + email + "|" + userId;
		codeid = RSAUtils.encryptString(codeid, EnvSetup.getData("REG_PUBLIC_KEY"));
		Logger.debug(RSAUtils.decryptString(codeid, EnvSetup.getData("REG_PRIVATE_KEY")));
		return codeid;

	}
	private void sendMail(Integer uid, String email, String loginid)
			throws NoSuchAlgorithmException, InvalidKeySpecException, GeneralSecurityException, ClientProtocolException, IOException {
		SendToEmailBean sendtoemailBean=new SendToEmailBean();
		SendToEmailCall sendtoemailCall=new SendToEmailCall();
		sendtoemailBean.setLId(uid);
		sendtoemailBean.setTomail(email);
		sendtoemailBean.setUId(loginid);
		sendtoemailCall.setData(sendtoemailBean);
		sendtoemailCall.callService();	

	}
}